<template>
  <div>
    <el-card class="box-card" style="margin:20px">
      <div slot="header" class="clearfix">
        <el-form :inline="true">
          <el-form-item label="系统名称" style="margin-bottom:0px;">
            <el-input v-model="searchName" placeholder="输入名称" />
          </el-form-item>
          <el-form-item label="系统编码" style="margin-bottom:0px;">
            <el-input v-model="seachForm.code" placeholder="输入名称" />
          </el-form-item>
          <el-form-item style="margin-bottom:0px;">
            <el-button type="primary" icon="el-icon-search" @click="getSystemList">查询</el-button>
            <el-button type="primary" icon="el-icon-edit" @click="add">新增</el-button>
          </el-form-item>
        </el-form>
      </div>
      <el-table v-loading="loading" :data="tableData" style="width: 100%">
        <el-table-column
          prop="id"
          label="系统编号(租户)"
          min-width="25%"
        />
        <el-table-column
          prop="name"
          label="系统名称"
          min-width="25%"
        />
        <el-table-column
          prop="code"
          label="系统编码"
          min-width="15%"
        />
        <el-table-column
          prop="updateTime"
          label="更新时间"
          min-width="15%"
        />
        <el-table-column
          align="center"
          label="操作"
          min-width="25%"
        >
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="warning"
              icon="el-icon-edit"
              @click="update(scope.$index, scope.row)"
            >
              修改
            </el-button>
            <el-button
              size="mini"
              type="danger"
              icon="el-icon-delete"
              @click="remove(scope.$index, scope.row)"
            >
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="block" style="float:right;margin:20px">
        <el-pagination
          :current-page="currentPage"
          :page-sizes="[10, 20, 50, 100]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
      <div>
        <el-dialog title="业务系统信息" :visible.sync="dialogFormVisible">
          <el-form ref="systemForm" label-width="100px" size="mini" :model="systemForm" :rules="rules">
            <el-form-item label="系统名称" prop="systemName">
              <el-input
                v-model="systemForm.systemName"
              />
            </el-form-item>
            <el-form-item label="系统编码" prop="systemCode">
              <el-input
                v-model="systemForm.systemCode"
                :disabled="updateSystemId>0"
              />
            </el-form-item>
            <el-form-item label="表单设计页地址" prop="formUrl">
              <el-input
                v-model="systemForm.formUrl"
              />
            </el-form-item>
            <el-form-item label="事项申请页地址" prop="applyUrl">
              <el-input
                v-model="systemForm.applyUrl"
              />
            </el-form-item>
            <el-form-item label="流程设计页地址" prop="flowUrl">
              <el-input
                v-model="systemForm.flowUrl"
              />
            </el-form-item>
            <el-form-item label="appSecret" prop="appSecret">
              <el-input
                v-model="systemForm.appSecret"
              />
            </el-form-item>
          </el-form>
          <div slot="footer" class="dialog-footer">
            <el-button @click="dialogFormVisible = false">取 消</el-button>
            <el-button type="primary" @click="saveOrUpdate">确 定</el-button>
          </div>
        </el-dialog>
      </div>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    var validateSystemCode = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入系统编码'))
      } else if (!(/^[0-9A-Za-z]{4,20}$/.test(value))) {
        // !(/^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{4,20}$/ 字母+数字
        callback(new Error('请输入4-20位数字或字母'))
      } else {
        if (this.systemForm.systemCode !== '') {
          const params = new FormData()
          params.append('code', this.systemForm.systemCode)
          params.append('updateSystemId', this.updateSystemId)
          this.$axios.post(this.basePortalUrl + '/system/validateSystemCode',
            params
          ).then(res => {
            if (res.data.data) {
              callback()
            } else {
              callback(new Error('系统编码已存在，请重新输入'))
            }
          }).catch(() => {
            callback(new Error('请输入系统编码'))
            this.$message.error('校验系统编码失败')
          })
        }
      }
    }
    return {
      tableData: [],
      rules: {
        systemName: [
          { required: true, message: '请输入系统名称', trigger: 'blur' }
        ],
        systemCode: [
          { required: true, validator: validateSystemCode, trigger: 'blur' }
        ]
      },
      currentPage: 1,
      pageSize: 10,
      total: 0,
      dialogFormVisible: false,
      updateSystemId: 0,
      searchName: '',
      systemForm: {
        systemName: '',
        systemCode: '',
        formUrl: '',
        applyUrl: '',
        flowUrl: ''
      },
      loading: false,
      seachForm: {
        code: ''
      }
    }
  },
  mounted() {
    this.getSystemList()
  },
  methods: {
    getSystemList() {
      this.loading = true
      var params = {
        'pageSize': this.pageSize,
        'pageNum': this.currentPage,
        'name': this.searchName,
        'code': this.seachForm.code
      }
      this.$axios.get(this.basePortalUrl + '/system/page', {
        params
      }).then(res => {
        this.tableData = res.data.data.records
        this.total = parseInt(res.data.data.total)
        this.loading = false
      }).catch(() => {
        this.loading = false
        this.$message.error('获取业务系统列表失败')
      })
    },
    handleSizeChange(val) {
      this.pageSize = val
      this.currentPage = 1
      this.getSystemList()
    },
    handleCurrentChange(val) {
      this.currentPage = val
      this.getSystemList()
    },
    add() {
      if (this.$refs['systemForm'] !== undefined) {
        this.$refs['systemForm'].resetFields()
      }
      this.$set(this.systemForm, 'systemName', '')
      this.$set(this.systemForm, 'systemCode', '')
      this.updateSystemId = 0
      this.dialogFormVisible = true
    },
    update(index, row) {
      if (this.$refs['systemForm'] !== undefined) {
        this.$refs['systemForm'].resetFields()
      }
      this.$set(this.systemForm, 'systemName', row.name)
      this.$set(this.systemForm, 'systemCode', row.code)
      this.$set(this.systemForm, 'formUrl', row.formUrl)
      this.$set(this.systemForm, 'applyUrl', row.applyUrl)
      this.$set(this.systemForm, 'flowUrl', row.flowUrl)
      this.$set(this.systemForm, 'appSecret', row.appSecret)
      this.updateSystemId = row.id
      this.dialogFormVisible = true
    },
    saveOrUpdate() {
      this.$refs['systemForm'].validate((valid) => {
        if (valid) {
          var params = {
            'name': this.systemForm.systemName,
            'code': this.systemForm.systemCode,
            'formUrl': this.systemForm.formUrl,
            'applyUrl': this.systemForm.applyUrl,
            'flowUrl': this.systemForm.flowUrl,
            'appSecret': this.systemForm.appSecret
          }
          var url = this.basePortalUrl + '/system/create'
          if (this.updateSystemId != 0) {//eslint-disable-line
            url = this.basePortalUrl + '/system/update'
            this.$set(params, 'id', this.updateSystemId)
          }
          this.$axios.post(url,
            params
          ).then(res => {
            this.$message.success('操作成功~')
            this.dialogFormVisible = false
            this.systemForm.systemCode = ''
            this.systemForm.systemName = ''
            this.systemForm.formUrl = ''
            this.systemForm.applyUrl = ''
            this.systemForm.flowUrl = ''
            this.updateSystemId = 0
            this.getSystemList()
          }).catch(() => {
            this.$message.error('操作失败！')
          })
        } else {
          this.$message.error('请正确输入')
          return false
        }
      })
    },
    remove(index, row) {
      this.$confirm('此操作将永久删除该业务系统, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
        center: true
      }).then(() => {
        this.$axios.delete(this.basePortalUrl + '/system/remove', {
          params: {
            id: row.id
          }
        }).then(res => {
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
          this.getSystemList()
        }).catch(() => {
          this.$message({
            type: 'error',
            message: '删除失败'
          })
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    }

  }
}
</script>
